package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.ProgramaService;
import co.gov.medellin.ssti.sub.model.service.SedeService;
import co.gov.medellin.ssti.sub.model.service.SubprogramaService;
import co.gov.medellin.ssti.sub.model.vo.ProgramaVO;
import co.gov.medellin.ssti.sub.model.vo.SedeVO;
import co.gov.medellin.ssti.sub.model.vo.SubprogramaVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;

public class UsuarioSubprogramaAction extends Action {
	@Autowired
	ProgramaService programaServiceImpl;
	@Autowired
	SedeService sedeServiceImpl;
	@Autowired
	SubprogramaService subprogramaServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		Object codePrograma = (Object) request.getParameter("codePrograma");
		Object codeSubprograma = request.getParameter("codeSubPrograma");

		HttpSession session = request.getSession();
		UserVO user = (UserVO) session.getAttribute("userInfo");
		session.removeAttribute("msg");

		session.setAttribute("msg", "");
		Collection<ProgramaVO> programas = null;
		Collection<SubprogramaVO> subprogramas = null;
		Collection<SedeVO> sedes = null;

		session.removeAttribute("Beneficios");

		// Object obAccion = request.getParameter("accion");
		session.removeAttribute("programas");
		session.removeAttribute("subprogramas");
		session.removeAttribute("sedes");

		try {
			//ProgramaVO vo = new ProgramaVO();
			if (codePrograma != null) {
				//vo.setCode(Long.valueOf((String) codePrograma));
				request.setAttribute("codePrograma", codePrograma);
			}
			programas = programaServiceImpl.findProgramaByCriteria(
					new ProgramaVO(), user.getCode(), user.getUserGroupName(),
					user.getCodeArea());
		} catch (DAOException e) {
			SUBException.logDebugError("", e);
		}
		session.removeAttribute("msg");
		if (programas.size() > 0) {
			session.setAttribute("programas", programas);
		} else {
			session.setAttribute("msg",
					"No se encontraron programas vigentes a la fecha..");
		}

		if (codePrograma != null) {

			String codpro = (String) codePrograma;
			if (!codpro.equals("nulo")) {
				try {
					SubprogramaVO subpro = new SubprogramaVO();
					subpro.setCodePrograma(new Long((String) codePrograma));
					subprogramas = subprogramaServiceImpl
							.findSubprogramaByCriteria(subpro, user.getCode(),
									user.getUserGroupName(), user.getCodeArea());
				} catch (DAOException e) {
					SUBException.logDebugError("", e);
				}
				session.removeAttribute("msg");
				if (subprogramas.size() > 0) {
					session.setAttribute("subprogramas", subprogramas);
				} else {
					session.setAttribute("msg",
							"No se encontraron subprogramas asociados a este programa que esten vigentes..");
				}

				if (codeSubprograma != null) {
					String codsubpro = (String) codeSubprograma;
					if (!codsubpro.equals("nulo")) {

						try {
							sedes = sedeServiceImpl
									.findTotalSedebyUser(user.getCode(),
											new Long(codsubpro),
											user.getUserGroupName(),
											user.getCodeArea());

						} catch (DAOException e) {
							SUBException.logDebugError("", e);
						}
						if (sedes.size() > 0) {
							session.setAttribute("sedes", sedes);

						} else {
							session.setAttribute("msg",
									"No se encontraron sedes vigentes a la fecha..");
						}

					}
				}
			} else {
				session.setAttribute("msg", "");
				session.removeAttribute("subprogramas");
			}
		}

		return mapping.findForward("success");

	}
}